<cfcomponent displayname="FusecacheWrapperNativeEHCache" output="false" hint="The FusecacheNativeWrapper is an object that is used to wrap ColdFusion 9's native caching (EHCache) in such a way that it conforms to Fusecache's method of dynamically setting/using caching engines.">
	
	<!--- init() --->
	<cffunction name="init" access="public" returntype="fusecache.FusecacheWrapperNativeEHCache">
		<cfreturn this />
	</cffunction>
	
	
	<!--- get() --->
	<cffunction name="get" access="public" output="false" returntype="any" hint="Wraps CF9's 'cacheGet' function.">
		<cfargument name="key" type="string" required="true" />
		<cfset var local = {} />
		
		<cftry>
			
			<!--- Try to cache the data --->
			<!--- We could do our compatibility testing first, but we're trying to keep things as fast as possible,
			so let's just try and do it and if there's an error we'll catch it and figure out what happened after
			the fact --->
			<cfset local.data = cacheGet(arguments.key) />
			
			<cfcatch type="any">
				<cfset testCompatibility(cfcatch) />
			</cfcatch>
			
		</cftry>
		
		<!--- If no errors, return cached data --->
		<cfreturn local.data />
		
	</cffunction>
	
	
	<!--- set() --->
	<cffunction name="set" access="public" output="false" returntype="boolean" hint="Wraps CF9's 'cachePut' function.">
		<cfargument name="key" type="string" required="true" />
		<cfargument name="value" type="any" required="true" />
		<cfargument name="expiry" type="numeric" required="true" />
		
		<cftry>
		
			<!--- Attempt to put data into the cache --->
			<cfset cachePut(arguments.key, arguments.value, createTimeSpan(0,0,0,arguments.expiry))>
			
			<!--- Catch errors --->
			<cfcatch type="any">
				<cfset testCompatibility(cfcatch) />
			</cfcatch>
			
		</cftry>
		
		<!--- If no errors, return true --->
		<cfreturn true />
		
	</cffunction>
	
	
	<!--- testCompatibility() --->
	<cffunction name="testCompatibility" access="private" output="false" returntype="void" hint="Runs logic to try and identify the nature of errors created when using the FusecacheNativeWrapper.">
		<cfargument name="originalError" type="any" required="true" />
		
		<!--- Confirm we're running ColdFusion --->
		<cfif (isColdFusion())>
		
			<!--- Confirm valid server version --->
			<cfif (NOT isSupportedVersion())>
				<cfthrow type="fusecache.UnsupportedServerVersion" message="You are attempting to use the native EHCache system that was included starting in ColdFusion version 9, but you are running #Server.ColdFusion.ProductVersion#." />
			<cfelse>
				<!--- Server is Coldfusion and version appears correct. Unknown error. Rethrow so we can see it. --->
				<cfthrow type="genericErrorMessage" message="#originalError.message#" />
			</cfif>

		<cfelse>
			<cfthrow type="fusecache.UnsupportedServerType" message="The FusecacheNativeWrapper is intended for use only with Adobe ColdFusion Server. It appears you are using a third party CFML engine." />
		</cfif>

	</cffunction>

	
	<!--- isColdFusion() --->
	<cffunction name="isColdFusion" access="private" output="false" returntype="boolean" hint="Looks to see if the server is running ColdFusion (as opposed to another CFML engine)">
		<cfscript>
			if (Server.ColdFusion.ProductName EQ "ColdFusion Server") { return true; }
			else { return false; }
		</cfscript>
	</cffunction>
	
	
	<!--- hasSupportedVersion() --->
	<cffunction name="isSupportedVersion" access="private" output="false" returntype="boolean" hint="Runs logic to determine whether the server supports ColdFusion 9 native caching.">
		<cfswitch expression="#ListGetAt(Server.ColdFusion.ProductVersion,1)#">
			<cfcase value="1,2,3,4,5,6,7,8" delimiters=",">
				<cfreturn false />
			</cfcase>
			<cfdefaultcase>
				<cfreturn true />
			</cfdefaultcase>
		</cfswitch>
	</cffunction>
	
	
</cfcomponent>